package com.download.service.impl;

import com.download.service.DownloadService;
import com.feign.util.FileUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
public class DownloadServiceImpl implements DownloadService {
    /**
     * 下载单个文件
     *
     * @param filePath 文件路径
     * @throws Exception
     */
    @Override
    public void downloadFile(String filePath) throws Exception {
        File file = FileUtil.fileExist(filePath);

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();

        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        response.addHeader("Content-Length", "" + new File(filePath).length());

        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream())
        ) {
            IOUtils.copy(inputStream, outputStream);
        }
    }

    /**
     * 打包下载文件
     * @param filePathList 文件路径集合
     * @param currentPath 当前所在路径
     * @throws Exception
     */
    @Override
    public void downloadZip(List<String> filePathList,String currentPath) throws IOException {
        List<File> fileList = new ArrayList<>();
        for (String path : filePathList) {
            File file = new File(path);
            if (!file.exists()) continue;

            List<File> searchFileAll = FileUtil.searchFileAll(file);
            List<File> collect = searchFileAll.stream().filter(item -> item.isFile()).collect(Collectors.toList());
            fileList.addAll(collect);
        }

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();

        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("download.zip", "UTF-8"));

        try (ZipOutputStream outputStream = new ZipOutputStream(response.getOutputStream());
        ) {
            for (File file : fileList) {
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
                ZipEntry zipEntry = new ZipEntry(file.getAbsolutePath().replace("\\","/").replace(currentPath.replace("\\","/"),""));
                outputStream.putNextEntry(zipEntry);

                IOUtils.copy(inputStream,outputStream);

                inputStream.close();
                outputStream.closeEntry();
            }
        }
    }

}
